package com.zuxia.impservices;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zuxia.iDao.DepartmentinfoDAOI;
import com.zuxia.iDao.PositioninfoDAOI;
import com.zuxia.iDao.PowerinfoDAOI;
import com.zuxia.iDao.StaffindoDAOI;
import com.zuxia.iservices.PeopleManagementServicesI;
import com.zuxia.po.Departmentinfo;

import com.zuxia.po.Positioninfo;
import com.zuxia.po.Powerinfo;
import com.zuxia.po.Staffindo;
import com.zuxia.util.PageModel;
/**
 * 人员管理业务实现类
 * @author 谌华勇
 *
 * 2018年11月12日下午6:58:42
 */
public class PeopleManagementServices implements PeopleManagementServicesI {
	/**
	 * 部门dao接口
	 */
	private DepartmentinfoDAOI departmentinfoDAO;
	/**
	 * 职业dao接口
	 */
	private PositioninfoDAOI positioninfoDAO;
	
	/**
	 * 员工dao接口
	 */
	private StaffindoDAOI staffindoDAO;
	
	/**
	 * 权限接口
	 */
	private PowerinfoDAOI powerinfoDAO;
	
	public List findAllDepartmentFordpIidAnddpIname() {
		return departmentinfoDAO.findAllDepartmentFordpIidAnddpIname(); 
	}
	public List findAll() {
		System.out.println("开始查询所有部门");
		return departmentinfoDAO.findAll(); 
	}
	public String addstaffInfo(String name,String selSex,String inpPhone,String selDepartment,String selPosition,String inpCardId,String txtAddress) {
		//构建成员工实体对象
		String pwd = inpCardId.substring(inpCardId.length()-6, inpCardId.length());
		
		Positioninfo positioninfo = positioninfoDAO.findById(Integer.parseInt(selPosition));
		
		Departmentinfo departmentinfo = departmentinfoDAO.findById(Integer.parseInt(selDepartment));
		Powerinfo powerinfopo = (Powerinfo) powerinfoDAO.findByProperty("piname", "员工").get(0);
		Staffindo staffindoPo = new Staffindo(positioninfo, departmentinfo,powerinfopo, name, selSex, pwd, inpPhone, inpCardId, txtAddress);
		int row =staffindoDAO.save(staffindoPo);
		if (row==1) {
			return "添加成功";
		}
		return "添加失败，可能信息重复或者该职位已有人！";
	}
	
	
	public String addDepartmentinfo(String name,String txtdpIduty) {
		//构建成部门实体对象
		Departmentinfo departmentinfoPo = new Departmentinfo(name, txtdpIduty);
		int row =departmentinfoDAO.save(departmentinfoPo);
		if (row==1) {
			return "添加成功";
		}
		return "添加失败，该部门名称重复！";
	}
	
	public String updatestaffInfo(String name, String selSex, String inpPhone, String selDepartment, String selPosition,
			String inpCardId, String txtAddress, Integer siid) {
		//构建成员工实体对象
				String pwd = inpCardId.substring(inpCardId.length()-6, inpCardId.length());
				
				Positioninfo positioninfo = positioninfoDAO.findById(Integer.parseInt(selPosition));
				
				Departmentinfo departmentinfo = departmentinfoDAO.findById(Integer.parseInt(selDepartment));
				Powerinfo powerinfopo = (Powerinfo) powerinfoDAO.findByProperty("piname", "员工").get(0);
				Staffindo staffindoPo = new Staffindo(siid,positioninfo, departmentinfo,powerinfopo, name, selSex, pwd, inpPhone, inpCardId, txtAddress);
				int row =staffindoDAO.merge(staffindoPo);
				if (row==1) {
					return "修改成功";
				}
				return "修改失败，信息重复！";
	}
	
	public String updateDepar(String name,String txtdpIduty, Integer dpIid) {
			//构建成部门实体对象
				Departmentinfo departmentinfoPo = new Departmentinfo(dpIid, name, txtdpIduty);
				int row =departmentinfoDAO.merge(departmentinfoPo);
				if (row==1) {
					return "修改成功";
				}
				return "修改失败，信息重复！";
	}
	
	
	public String delStaffById(Integer siid) {
		//得到员工实体
		Staffindo staffindo = staffindoDAO.findById(siid);
		int row =staffindoDAO.delete(staffindo);
		if (row==1) {
			return "删除成功";
		}
		return "删除失败！";
	}
	
	public String delDeparfById(Integer dpIid) {
		//得到部门实体
		Departmentinfo departmentinfoPo = departmentinfoDAO.findById(dpIid);
		int row =departmentinfoDAO.delete(departmentinfoPo);
		if (row==1) {
			return "删除成功";
		}
		return "删除失败！";
	}
	public String selPositionOnlyId_Name(String dpIid) {
		List lispPonsition = positioninfoDAO.showPositionOnlyIdAndName(dpIid);
		String json = JSONArray.toJSONString(lispPonsition);
		return json;
	}
	
	public String selStaffByPageModel(String pageNumber, String limit, String serachstuName) {
		//先得到员工信息总数
		int total = staffindoDAO.selAllCout(serachstuName);
		//int total = 10;
		//得到员工信息
		List<Staffindo> list = staffindoDAO.selAllStaInfoByPageModel(pageNumber, limit, serachstuName);
		System.out.println("888888");
		System.out.println("--"+list.get(0).getDepartmentinfo().getDpIname());
		//将分页参数存入分页工具类中
				PageModel<Staffindo> pModelStaffindo = new PageModel<Staffindo>(total, Integer.parseInt(pageNumber),Integer.parseInt(limit));
				//将分页内容存入工具类中
				pModelStaffindo.setList(list);
				System.out.println("分页信息如下！！！");
				System.out.println(pModelStaffindo.getPageNumber());
				System.out.println(pModelStaffindo.getLimit());
				System.out.println(pModelStaffindo.getPages());
				System.out.println(pModelStaffindo.getTotal());
				System.out.println(pModelStaffindo.getList());
				System.out.println(pModelStaffindo.getNavigatePageNumbers().toString());
				System.out.println(pModelStaffindo.toString());
				//第二个参数禁用FastJson的“循环引用检测”特性。（防止前台的json出现$ref引用）
				String json = JSONArray.toJSONString(pModelStaffindo, SerializerFeature.DisableCircularReferenceDetect);
				System.out.println("以下是json字符串");
				System.out.println(json);
		
		return json;
	}
	
	public String selDeparByPageModel(String pageNumber, String limit, String serachstuName) {
		//先得到部门信息总数
		int total = departmentinfoDAO.selAllCout(serachstuName);
		//int total = 10;
		//得到部门信息
		List<Departmentinfo> list = departmentinfoDAO.selAllDeparByPageModel(pageNumber, limit, serachstuName);
		System.out.println("888888");
		//将分页参数存入分页工具类中
				PageModel<Departmentinfo> pModelStaffindo = new PageModel<Departmentinfo>(total, Integer.parseInt(pageNumber),Integer.parseInt(limit));
				//将分页内容存入工具类中
				pModelStaffindo.setList(list);
				System.out.println("分页信息如下！！！");
				System.out.println(pModelStaffindo.getPageNumber());
				System.out.println(pModelStaffindo.getLimit());
				System.out.println(pModelStaffindo.getPages());
				System.out.println(pModelStaffindo.getTotal());
				System.out.println(pModelStaffindo.getList());
				System.out.println(pModelStaffindo.getNavigatePageNumbers().toString());
				System.out.println(pModelStaffindo.toString());
				//第二个参数禁用FastJson的“循环引用检测”特性。（防止前台的json出现$ref引用）
				String json = JSONArray.toJSONString(pModelStaffindo, SerializerFeature.DisableCircularReferenceDetect);
				System.out.println("以下是json字符串");
				System.out.println(json);
		return json;
	}
	public List selPosition() {
		
		return null;
	}
	
	
	
	

	public DepartmentinfoDAOI getDepartmentinfoDAO() {
		return departmentinfoDAO;
	}

	public void setDepartmentinfoDAO(DepartmentinfoDAOI departmentinfoDAO) {
		this.departmentinfoDAO = departmentinfoDAO;
	}

	public PositioninfoDAOI getPositioninfoDAO() {
		return positioninfoDAO;
	}

	public void setPositioninfoDAO(PositioninfoDAOI positioninfoDAO) {
		this.positioninfoDAO = positioninfoDAO;
	}
	
	//删除
	public boolean delect(Departmentinfo persistentInstance) {
		departmentinfoDAO.delete(persistentInstance);
		// TODO Auto-generated method stub
		return true;
	}
	public StaffindoDAOI getStaffindoDAO() {
		return staffindoDAO;
	}
	public void setStaffindoDAO(StaffindoDAOI staffindoDAO) {
		this.staffindoDAO = staffindoDAO;
	}
	public PowerinfoDAOI getPowerinfoDAO() {
		return powerinfoDAO;
	}
	public void setPowerinfoDAO(PowerinfoDAOI powerinfoDAO) {
		this.powerinfoDAO = powerinfoDAO;
	}


}
